package com.example.xsssglxt.controller;

import com.alibaba.excel.EasyExcel;
import com.example.xsssglxt.entity.Student;
import com.example.xsssglxt.entity.Sushe;
import com.example.xsssglxt.mapper.SusheMapper;
import com.example.xsssglxt.service.SusheService;
import io.swagger.models.auth.In;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Controller
@RequestMapping("/sushe")
public class SusheController {
    @Autowired
    private SusheMapper susheMapper;
    @Autowired
    private SusheService susheService;
    @GetMapping("/index")
    public Map<String,Object> index(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String number){
    pageNum=(pageNum-1)*pageSize;
        Map<String,Object> res=new HashMap<>();
        List<Sushe> data=susheMapper.selectpage(pageNum,pageSize,number);
        Integer total=susheMapper.total(number);
        res.put("data",data);
        res.put("total",total);
        res.put("code",200);
return res;
    }
    @GetMapping("/indexx")
    public Map<String,Object> indexx(){

        Map<String,Object> res=new HashMap<>();
        List<Sushe> data=susheMapper.selectall();

        res.put("data",data);
        res.put("code",200);
        return res;
    }
    @PostMapping("/add")
    public  Map<String ,Object> add(@RequestBody Sushe sushe){
        Map<String,Object> res=new HashMap<>();
        if(susheService.sushe(sushe) == 1){
            res.put("code",200);
            res.put("msg","新增成功");

        }
        return res;
    }
    @PostMapping("/update")
    public Map<String ,Object> update(@RequestBody Sushe sushe){
        Map<String,Object> res=new HashMap<>();
        if(susheService.changesushe(sushe) ==1){
            res.put("code",200);
            res.put("msg","修改成功");
        }
        return  res;
    }
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable Integer id){
        Map<String,Object> res=new HashMap<>();
        Integer col=susheMapper.deleter(id);
        if(col == 1){
            res.put("code",200);
            res.put("msg","操作成功");

        }
        return res;

    }
    @GetMapping("/down")
    public Result down(HttpServletResponse response) throws IOException {
        List<Sushe> studentList=susheMapper.selectall();
        response.setContentType("application/octet-stream");
        String fileName= URLEncoder.encode("宿舍表","UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(),Sushe.class).autoCloseStream(Boolean.FALSE).sheet("宿舍表").doWrite(studentList);


        return null;
    }
}
